from dataclasses import dataclass
from typing import Any, Generic, TypeVar, Optional

from speech_convert.models.response import Response, ResponseCode

T = TypeVar("T")


@dataclass
class ResponseUtils(Response, Generic[T]):
    """通用响应类"""

    data: Optional[T] = None

    @classmethod
    def success(
        cls, data: Optional[T] = None, message: str = "操作成功"
    ) -> "ResponseUtils[T]":
        """创建成功响应"""
        return cls(code=ResponseCode.SUCCESS, message=message, data=data)

    @classmethod
    def status_update(
        cls, status: str, details: Any = None, message: str = "状态更新"
    ) -> "ResponseUtils[T]":
        """创建状态响应"""
        return cls(
            code=ResponseCode.SUCCESS, status=status, message=message, data=details
        )

    @classmethod
    def error(
        cls, code: int = ResponseCode.INTERNAL_ERROR, message: str = "操作失败"
    ) -> "ResponseUtils[T]":
        """创建错误响应"""
        return cls(code=code, message=message, data=None)

    @classmethod
    def bad_request(cls, message: str = "请求参数错误") -> "ResponseUtils[T]":
        """创建400错误响应"""
        return cls.error(ResponseCode.BAD_REQUEST, message)

    @classmethod
    def unauthorized(cls, message: str = "未授权访问") -> "ResponseUtils[T]":
        """创建401错误响应"""
        return cls.error(ResponseCode.UNAUTHORIZED, message)

    @classmethod
    def forbidden(cls, message: str = "禁止访问") -> "ResponseUtils[T]":
        """创建403错误响应"""
        return cls.error(ResponseCode.FORBIDDEN, message)

    @classmethod
    def not_found(cls, message: str = "资源未找到") -> "ResponseUtils[T]":
        """创建404错误响应"""
        return cls.error(ResponseCode.NOT_FOUND, message)

    @classmethod
    def internal_error(cls, message: str = "内部服务器错误") -> "ResponseUtils[T]":
        """创建500错误响应"""
        return cls.error(ResponseCode.INTERNAL_ERROR, message)

    @classmethod
    def service_unavailable(cls, message: str = "服务不可用") -> "ResponseUtils[T]":
        """创建503错误响应"""
        return cls.error(ResponseCode.SERVICE_UNAVAILABLE, message)
